﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Helper;
using SaleSoft.Models.ScreenClient;
using SaleSoft.Constants;
using Telerik.Web.Mvc;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class TrangThaiController : BaseController {
        #region Properties Section
        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            TrangThaiSceenClient screen = new TrangThaiSceenClient();
            if (Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT] != null) {
                screen = (TrangThaiSceenClient)Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            ViewBag.id_loaitrangthai = SelectListHelper.DSLoaiTrangThai(null);
            return View(screen);
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Index ( TrangThaiSceenClient screen ) {
            Search(screen);
            ViewBag.id_loaitrangthai = SelectListHelper.DSLoaiTrangThai(null);
            return View(new GridModel(screen.DSTrangThai));
        }
        #endregion

        #region CRUD Section

        [HttpPost]
        [GridAction]
        public ActionResult _Insert () {
            SaleSoft.Models.BaseClient.TrangThai trangthaiClient = new Models.BaseClient.TrangThai();
            ds_trangthai trangthai = new ds_trangthai();
            TryUpdateModel(trangthaiClient);
            ObjectHelper.Copy(trangthaiClient, trangthai);
            db.ds_trangthai.AddObject(trangthai);
            db.SaveChanges();

            TrangThaiSceenClient screen = new TrangThaiSceenClient();
            if (Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT] != null) {
                screen = (TrangThaiSceenClient)Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSTrangThai));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Edit (int id) {
            SaleSoft.Models.BaseClient.TrangThai trangthaiClient = new Models.BaseClient.TrangThai();
            ds_trangthai trangthai = db.ds_trangthai.Single(p => p.id_trangthai == id);
            TryUpdateModel(trangthaiClient);
            trangthaiClient.id_trangthai = id;
            ObjectHelper.Copy(trangthaiClient, trangthai);
            db.ObjectStateManager.ChangeObjectState(trangthai, EntityState.Modified);
            db.SaveChanges();

            TrangThaiSceenClient screen = new TrangThaiSceenClient();
            if (Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT] != null) {
                screen = (TrangThaiSceenClient)Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSTrangThai));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _Delete ( int id ) {
            ds_trangthai trangthai = db.ds_trangthai.Single(p => p.id_trangthai == id);
            try {
                db.ds_trangthai.DeleteObject(trangthai);
                db.SaveChanges();
            } catch {
            }
            TrangThaiSceenClient screen = new TrangThaiSceenClient();
            if (Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT] != null) {
                screen = (TrangThaiSceenClient)Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSTrangThai));
        }

        #endregion

        #region Helper Section
        private void Search ( TrangThaiSceenClient screen ) {
            var data = db.ds_trangthai.OrderBy(p => p.tentrangthai);
            screen.DSTrangThai.Clear();
            foreach (ds_trangthai trangthai in data) {
                SaleSoft.Models.BaseClient.TrangThai trangthaiClient = new Models.BaseClient.TrangThai();
                ObjectHelper.Copy(trangthai, trangthaiClient);
                trangthaiClient.TenLoaiTrangThai = trangthai.loai_trangthai.tenloaitrangthai;
                screen.DSTrangThai.Add(trangthaiClient);
            }

            Session[ScreenClientConstants.TRANG_THAI_SCREENCLIENT] = screen;
        }
        #endregion

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
    }
}